package com.example.web.websystem.controller;

import com.example.web.websystem.ann.AnnPaging;
import com.example.web.websystem.controller.vo.form.AddGroupFormVO;
import com.example.web.websystem.dao.model.SysGroupDO;
import com.example.web.websystem.dao.model.SysUserDO;
import com.example.web.websystem.response.CommonReturnData;
import com.example.web.websystem.service.SysGroupService;
import com.example.web.websystem.util.DozerUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by YangGang on 2021/4/6 14:01。
 */
@Api(tags = "组织管理")
@ApiSupport(author = "YangGang")
@RestController
@RequestMapping("/sys/group")
public class SysGroupController {
    @Autowired
    private SysGroupService sysGroupService;

    @ApiOperationSupport(order = 1, author = "YangGang")
    @ApiOperation("查询-全部组织列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", required = true),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页条数", required = true)
    })
    @AnnPaging()
    @GetMapping("/listGroups")
    public CommonReturnData listGroups() {
        List<SysGroupDO> sysGroups = sysGroupService.listAllSysGroup();
        return CommonReturnData.success(sysGroups);
    }

    @ApiOperationSupport(order = 2, author = "YangGang")
    @ApiOperation("查询-有效的组织列表（未删除的）")
    @GetMapping("/listNormalGroups")
    public CommonReturnData listNormalGroups() {
        List<SysGroupDO> sysGroups = sysGroupService.listAllSysGroup();
        List<SysGroupDO> list = sysGroups.stream().filter(g -> !g.getIsDeleted()).collect(Collectors.toList());
        return CommonReturnData.success(list);
    }

    @ApiOperation("添加-新组织")
    @PostMapping("/addGroup")
    public CommonReturnData addGroup(AddGroupFormVO formVO) {
        SysGroupDO sysGroup = new SysGroupDO();
        sysGroup.setGroupName(formVO.getGroupName());
        sysGroup.setGroupPaths(formVO.getGroupPaths());
        if (formVO.getUserId() != null) {
            sysGroup.setGroupAdminUserId(formVO.getUserId());
            sysGroupService.addSysGroup(sysGroup, null);
        } else {
            SysUserDO sysUserDO = DozerUtil.map(formVO, SysUserDO.class);
            sysGroupService.addSysGroup(sysGroup, sysUserDO);
        }
        return CommonReturnData.success(sysGroup.getGroupId());
    }

    @ApiOperation("删除-组织")
    @ApiImplicitParam(paramType = "query", name = "groupId", value = "组织ID", required = true)
    @PostMapping("/deleteGroup")
    public CommonReturnData deleteGroup(long groupId) {
        int count = sysGroupService.deleteSysGroup(groupId);
        return CommonReturnData.success(count);
    }

    @ApiOperation("恢复-组织")
    @ApiImplicitParam(paramType = "query", name = "groupId", value = "组织ID", required = true)
    @PostMapping("/restoreGroup")
    public CommonReturnData restoreGroup(long groupId) {
        int count = sysGroupService.restoreSysGroup(groupId);
        return CommonReturnData.success(count);
    }


    @ApiOperation("修改-组织名称")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "groupId", value = "组织ID", required = true),
            @ApiImplicitParam(paramType = "query", name = "groupName", value = "新的组织名称", required = true)
    })
    @PostMapping("/updateGroupName")
    public CommonReturnData updateGroupName(long groupId, String groupName) {
        SysGroupDO sysGroup = new SysGroupDO();
        sysGroup.setGroupName(groupName);
        sysGroup.setGroupId(groupId);
        int count = sysGroupService.updateSysGroup(sysGroup);
        return CommonReturnData.success(count);
    }

}
